package com.xpu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xpu.reggie.common.R;
import com.xpu.reggie.dto.DishDto;
import com.xpu.reggie.entity.Category;
import com.xpu.reggie.entity.Dish;
import com.xpu.reggie.entity.DishFlavor;
import com.xpu.reggie.service.CategoryService;
import com.xpu.reggie.service.DishFlavorService;
import com.xpu.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 菜品信息分页查询
     */
    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        //构造分页构造器对象
        Page<Dish> pageInfo1 = new Page<>(page, pageSize);
        //条件构造器
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        //添加排序条件
        queryWrapper.orderByDesc("update_time");
        //执行分页查询
        dishService.page(pageInfo1, queryWrapper);

        //对象拷贝
        Page<DishDto> pageInfo2 = new Page<>(page, pageSize);

        //拷贝第一部分数据
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : pageInfo1.getRecords()) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //根据id查询分类对象
            Category category = categoryService.getById(dish.getCategoryId());
            //设置分类名称
            dishDto.setCategoryName(category.getName());
            //将dishDto添加到集合中
            dishDtoList.add(dishDto);
        }
        pageInfo2.setRecords(dishDtoList);

        //拷贝第二部分数据
        BeanUtils.copyProperties(pageInfo1, pageInfo2, "records");


        return R.success(pageInfo2);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     */
    @GetMapping("{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品信息成功");
    }

    /**
     * 根据条件查询菜品列表
     */
    @GetMapping("list")
    public R<List<DishDto>> list(Dish dish) {
        //构造查询条件
        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq(dish.getCategoryId() != null, "category_id", dish.getCategoryId());
        queryWrapper1.eq(StringUtils.isNotBlank(dish.getName()), "name", dish.getName());
        queryWrapper1.eq("status", 1);
        queryWrapper1.orderByAsc("sort").orderByAsc("update_time");
        //查询列表
        List<Dish> dishList = dishService.list(queryWrapper1);

        //将每个Dish对象转换为DishDto，放入dishDtoList集合中，响应
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish item : dishList) {
            DishDto dishDto = new DishDto();
            //基本属性拷贝
            BeanUtils.copyProperties(item, dishDto);
            //根据菜品id查询对应的口味集合
            QueryWrapper<DishFlavor> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("dish_id", item.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper2);
            //设置菜品口味的数据
            dishDto.setFlavors(dishFlavorList);
            dishDtoList.add(dishDto);
        }

        return R.success(dishDtoList);
    }

}